package com.erdos.core.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.erdos.common.service.BaseCRUDService;
import com.erdos.core.dao.RefTableDao;
import com.erdos.core.entity.RefTable;
import com.erdos.core.entity.RefTableDetail;
import com.erdos.core.service.FieldDefinitionService;
import com.erdos.core.service.RefTableDetailService;
import com.erdos.core.service.RefTableService;

public class RefTableServiceImpl extends BaseCRUDService<RefTable> implements
		RefTableService {
	private static final Log logger = LogFactory.getLog(RefTableServiceImpl.class);
	private RefTableDao refTableDao ;
	private FieldDefinitionService fieldDefinitionService ;
	private RefTableDetailService refTableDetailService;
	

	public RefTableDao getRefTableDao() {
		return refTableDao;
	}

	public void setRefTableDao(RefTableDao refTableDao) {
		this.refTableDao = refTableDao;
	}

	/**
	 * 查找ReferenceTable表中是否有ReferenceTableID重复的记录
	 * @param refTableId		ReferenceTableID字段的值
	 * @return						true/false
	 * @author LvXiaoGuang
	 * @since  2008-7-28
	 */
	public boolean checkRefTableExist(String refTableId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("refTableId", refTableId);
		logger.debug("check Reference Table Unique...");
		RefTable refEntity = null;
		if (StringUtils.isNotEmpty(refTableId)) {
			refEntity = getRefTableDao().findUniqueByParams(params);
			if (refEntity != null) {
				return true;
			} else {
				return false;
			}
		}else{
			return false;
		}
	}

	/**
	 * 更改ReferenceTable内数据的列定义时,判断与之前的列定义是否兼容
	 * @param sid				ReferenceTable表的主键SID
	 * @param column			修改定义的列名
	 * @param oldFieldId		旧的列定义
	 * @param newFieldId		新的列定义
	 * @return						true/false
	 * @author LvXiaoGuang
	 * @since  2008-7-28
	 */
	public boolean checkChangeCompatible(String sid, String column,String oldFieldId,String newFieldId) {
		Map<String, Object> params = new HashMap<String, Object>();
		//设置查询条件
		params.put("refTableSid", sid);
		logger.debug("check is compatible...");
		
		if (StringUtils.isNotEmpty(sid)) {
			//判断ReferenceTableDetail表中是否有数据
			List<RefTableDetail> tempList = getRefTableDetailService().findByParams(params) ;
			if(tempList!=null && tempList.size()>0) {
				//如果有数据  再加入列的限制条件
				params.put("columnName", column) ;
				
				//查询符合条件的记录
				List<RefTableDetail> tempDetailList = getRefTableDetailService().findByParams(params) ;
				if(tempDetailList!=null && tempDetailList.size()>0){
					
					//调用FieldDefinitionService中的isCompatible()方法判断兼容性
					boolean flag = getFieldDefinitionService().isCompatible(new Long(oldFieldId), new Long(newFieldId)) ;
					return flag;
				}else{
					//该列无数据,兼容
					return true ;
				}
			}else{
				//Detail表中无对应数据,兼容
				return true ;
			}
		}else{
			logger.debug("sid is null  exit..") ;
			return true;
		}
	}
	
	/**
	 *删除ReferenceTable表中的记录时,级联删除ReferenceTableDetail表中关联的数据
	 * @param sid				ReferenceTable表的主键SID
	 * @author LvXiaoGuang
	 * @since  2008-7-28 
	 */
	public int deleteCascadeDetail(String sid) {
		logger.debug("deleteCascadeDetail start sid="+sid) ;
		Map<String,Object> params = new HashMap<String,Object>() ;
		//设置查询条件
		params.put("refTableSid",sid) ;
		
		//根据ReferenceTable表的主键sid查找ReferenceTableDetail表中对应的所有记录
		List<RefTableDetail> list = getRefTableDetailService().findByParams(params) ;
		for(RefTableDetail detail:list) {
			
			//删除ReferenceTableDetail表中的数据
			this.refTableDetailService.delete(detail) ;			
		}
		
		//删除ReferenceTable表中的数据
		RefTable tempEntity = findByPrimaryKey(new Long(sid));
		int deleteRecord = delete(tempEntity);
		logger.debug("deleteCascadeDetail  Success!") ;		
		return deleteRecord ;
	}


	public FieldDefinitionService getFieldDefinitionService() {
		return fieldDefinitionService;
	}

	public void setFieldDefinitionService(
			FieldDefinitionService fieldDefinitionService) {
		this.fieldDefinitionService = fieldDefinitionService;
	}

	public RefTableDetailService getRefTableDetailService() {
		return refTableDetailService;
	}

	public void setRefTableDetailService(RefTableDetailService refTableDetailService) {
		this.refTableDetailService = refTableDetailService;
	}
	
	
}
